package com.onix.scalecamerapreview.translate;

import android.os.Build;
import android.text.TextUtils;


import com.onix.scalecamerapreview.MyApplication;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;


public class LanguageUtils {
    private LanguageUtils(){}

    private static class SingleHolder{
        private static LanguageUtils utils = new LanguageUtils();
    }

    public static LanguageUtils getInstance(){
        return SingleHolder.utils;
    }

    //获取本地语言
    public String getLocalLanguage(){
        Locale locale;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
        {
            Locale.getDefault().getCountry();
            locale = MyApplication.getInstance().getApplicationContext().getResources().getConfiguration().getLocales().get(0);
        } else
        { locale = MyApplication.getInstance().getApplicationContext().getResources().getConfiguration().locale;
        }
        if(locale!=null){
            String language = locale.getLanguage();
            if(!TextUtils.isEmpty(language)){
                return language;
            }else {
                return "en";
            }

        }
        return null;
    }


    /**
     * 根据简写获取对应的LanguageInfo
     */
    public LanguageInfo getEnname(String localLanguage,List<LanguageInfo> languageInfos){
        LanguageInfo enname = null;
        for (LanguageInfo languageInfo : languageInfos) {
            if(languageInfo.getInitial().equals(localLanguage)){
                enname = languageInfo;
                break;
            }
        }
        return enname;
    }



    /**
     * 获取最近使用语言的简写
     * @return
     */
    public String getRecentLanguage(){
//        String recentLg = SpUtils.obtain().getString("recentLg", null);
//        if(recentLg==null){
//            return getLocalLanguage();
//        }
//        return recentLg;
        return getLocalLanguage();
    }

    /**
     * 保存最近使用语言的简写
     * @param language
     */
    public void setRecentLanguage(String language) {
//         SpUtils.obtain().save("recentLg", language);
    }

}
